<template>
  <div class="container">
    <div class="loginbox">
      <form @submit.prevent="handleLogin" class="form">
        <div class="form-item">
          <label>用户名</label>
          <input v-model="ruleForm.username" placeholder="请输入用户名" />
        </div>
        <div class="form-item">
          <label>密码</label>
          <input
            v-model="ruleForm.password"
            placeholder="请输入密码"
            type="password"
          />
        </div>
        <div class="form-item">
          <label>手机号</label>
          <input v-model="ruleForm.phone" placeholder="请输入手机号" />
        </div>

        <div class="form-item">
          <label>验证码</label>
          <input v-model="ruleForm.asd" placeholder="请输入验证码" />
          <p v-html="asd"></p>
          <button type="button" @click="code">换个验证码</button>
        </div>

        <div class="form-buttons">
          <button type="submit" @click="login">登录</button>
          <button type="button" @click="handleRegister">注册</button>
        </div>
        <!-- <button @click="code()">sdfs</button> -->
      </form>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from "vue";
import axios from "axios";
const asd = ref("");
const qwe = ref("");
// 表单数据
const ruleForm = ref({
  username: "admin",
  password: "123456",
  phone: "16631069126",
  asd: "",
});
const rules = ref({});

// 验证码相关
const imageUrl = ref("");
const captchaData = ref({});

function code() {
  axios.get("http://localhost:3000/my/code").then((val) => {
    asd.value = val.data.asd;
    qwe.value = val.data.qwe;
    console.log(asd.value, qwe.value);
  });
}
function login() {
  console.log("====================================");
  console.log(ruleForm.value);
  console.log("====================================");
  axios
    .post("http://localhost:3000/my/login", {
      username: ruleForm.value.username,
      password: ruleForm.value.password,
      asd: ruleForm.value.asd,
      qwe: qwe.value,
    })
    .then((val) => {
      // console.log(val.data)
      if (val.data.code == 200) {
        if (qwe.value == ruleForm.value.asd) {
          localStorage.setItem("token", val.data.token);
          alert("登录成功");
          //  this.$router.push('/home')
        } else {
          alert("验证码错误");
        }
      } else {
        alert(val.data.msg);
      }
    });
}
function fetchImage() {
  console.log("====================================");
  console.log(ruleForm.value.username);
  console.log("====================================");
}

onMounted(code);
</script>

<style scoped>
.container {
  height: 97vh;
  background-color: antiquewhite;
  background-image: url('../../public/凤凰.gif');
  background-size: cover;
  background-repeat: no-repeat;
  display: flex;
  justify-content: center;
  align-items: center;
}

.loginbox {
  width: 270px;
  height: 550px;
  /* background-color: white; */
  background-color: rgba(255, 255, 255, 0.8); /* 白色半透明背景 */
  border-radius: 5px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  padding: 20px;
  box-sizing: border-box;
}

.form {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.form-item {
  margin-bottom: 20px;
  width: 100%;
}

.form-item label {
  display: block;
  margin-bottom: 5px;
}

.form-item input {
  width: 100%;
  padding: 10px;
  box-sizing: border-box;
}

.captcha-img {
  vertical-align: middle;
  margin-left: 10px;
}

.form-buttons {
  display: flex;
  justify-content: center;
  margin-top: 20px;
}

.form-buttons button {
  margin: 0 10px;
  padding: 10px 20px;
  cursor: pointer;
}
</style>
